using System;
using System.Web.Mvc;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;
using Netkk.Core;
using Netkk.Core.Auth;
using Netkk.Models;

namespace Netkk.Controllers
{
    public class ContactController : BaseControler
    {
        private readonly IMessageService messageService;
        private readonly IUserService userService;

        public ContactController(
            IMessageService messageService, 
            IUserService userService)
        {
            this.messageService = messageService;
            this.userService = userService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Contact()
        {
            User user = new CustomIdentity(ControllerContext.HttpContext.User.Identity).User;

            UserModel fromUser = user == null ?
                UserModel.Create() :
                UserModel.Create(user);
            MessageModel model = MessageModel.Create(fromUser);

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Contact(FormCollection formValues)
        {
            User fromUser = new CustomIdentity(ControllerContext.HttpContext.User.Identity).User;
            fromUser = fromUser ?? new User
                                       {
                                           Name = formValues["Name"],
                                           Email = formValues["Email"]
                                       };

            MessageModel model = MessageModel.Create(UserModel.Create(fromUser));
            model.ToUser = UserModel.Create(new User
                                                {
                                                    Name = ".NET Stammtisch",
                                                    Email = "juergen@gutsch-online.de"
                                                });

            try
            {
                UpdateModel(model, new[] { "Name", "Email", "Subject", "Text" });

                Message message = model.ToMessage();
                if (!messageService.SendMessage(message))
                {
                    return View(model);
                }

                return this.RedirectToActionWithId("Content", "Show", "ContactSuccessfull");
            }
            catch (Exception)
            {
                return View(model);
            }

        }

        [IsAuthenticated]
        public ActionResult Email(Guid id)
        {

            UserModel fromUser = UserModel.Create(new CustomIdentity(ControllerContext.HttpContext.User.Identity).User);
            UserModel toUser = UserModel.Create(userService.GetUserById(id));

            MessageModel model = MessageModel.Create(fromUser);
            model.ToUser = toUser;

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [IsAuthenticated]
        public ActionResult Email(Guid id, FormCollection formValues)
        {

            User fromUser = new CustomIdentity(ControllerContext.HttpContext.User.Identity).User;
            User toUser = userService.GetUserById(id);

            MessageModel model = MessageModel.Create(UserModel.Create(fromUser));
            model.ToUser = UserModel.Create(toUser);

            try
            {
                UpdateModel(model, new[] { "Name", "Email", "Subject", "Text" });

                Message mesasage = model.ToMessage();
                if (!messageService.SendMessage(mesasage))
                {
                    return View(model);
                }

                return this.RedirectToActionWithId("Content", "Show", "EmailSuccessfull");
            }
            catch (Exception)
            {
                return View(model);
            }

        }


    }
}
